package demo150;

import java.util.*;

// 定义进程类
class Process {
    String name; // 进程名
    int arrivalTime; // 到达时间
    int burstTime; // 执行时间
    int completionTime; // 完成时间
    int turnaroundTime; // 周转时间
    int waitingTime; // 等待时间
    int priority; // 优先级
    int remainingTime; // 剩余执行时间

    Process(String name, int arrivalTime, int burstTime, int priority) {
        this.name = name;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
        this.priority = priority;
        this.remainingTime = burstTime;
    }
}

public class ProcessScheduling {

    // 先来先服务调度算法
    public static void fcfsScheduling(List<Process> processes) {
        int currentTime = 0;
        for (Process process : processes) {
            if (process.arrivalTime > currentTime) {
                currentTime = process.arrivalTime;
            }
            currentTime += process.burstTime;
            process.completionTime = currentTime;
            process.turnaroundTime = process.completionTime - process.arrivalTime;
            process.waitingTime = process.turnaroundTime - process.burstTime;
        }
        printProcesses(processes);
    }

    // 短作业优先调度算法(非抢占式)
    public static void sjfScheduling(List<Process> processes) {
        processes.sort(Comparator.comparingInt(p -> p.burstTime));
        int currentTime = 0;
        for (Process process : processes) {
            if (process.arrivalTime > currentTime) {
                currentTime = process.arrivalTime;
            }
            currentTime += process.burstTime;
            process.completionTime = currentTime;
            process.turnaroundTime = process.completionTime - process.arrivalTime;
            process.waitingTime = process.turnaroundTime - process.burstTime;
        }
        printProcesses(processes);
    }

    // 时间片轮转调度算法
    public static void rrScheduling(List<Process> processes, int timeSlice) {
        Queue<Process> queue = new LinkedList<>(processes);
        int currentTime = 0;
        while (!queue.isEmpty()) {
            Process process = queue.poll();
            if (process.remainingTime > timeSlice) {
                currentTime += timeSlice;
                process.remainingTime -= timeSlice;
                queue.add(process);
            } else {
                currentTime += process.remainingTime;
                process.remainingTime = 0;
                process.completionTime = currentTime;
                process.turnaroundTime = process.completionTime - process.arrivalTime;
                process.waitingTime = process.turnaroundTime - process.burstTime;
            }
        }
        printProcesses(processes);
    }

    // 优先级调度算法
    public static void priorityScheduling(List<Process> processes) {
        processes.sort(Comparator.comparingInt(p -> p.priority));
        fcfsScheduling(processes);
    }

    // 打印进程信息
    public static void printProcesses(List<Process> processes) {
        System.out.println("Process Name|Arrival|Burst|Priority|Completion|Turnaround|Waiting");
        for (Process process : processes) {
            System.out.printf("%s\t\t\t\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\t\t%d\n",
                    process.name, process.arrivalTime, process.burstTime, process.priority,
                    process.completionTime, process.turnaroundTime, process.waitingTime);
        }
    }

    public static void main(String[] args) {
        List<Process> processes = Arrays.asList(
                new Process("P1", 0, 24, 3),
                new Process("P2", 4, 3, 1),
                new Process("P3", 5, 3, 2),
                new Process("P4", 6, 12, 4),
                new Process("P5", 10, 16, 5)
        );

        System.out.println("先来先服务调度算法:");
        fcfsScheduling(new ArrayList<>(processes));

        System.out.println("\n短作业优先调度算法:");
        sjfScheduling(new ArrayList<>(processes));

        System.out.println("\n时间片轮转调度算法(时间片 = 4):");
        rrScheduling(new ArrayList<>(processes), 4);

        System.out.println("\n优先级调度算法:");
        priorityScheduling(new ArrayList<>(processes));
    }
}
